<?php
/**
 *Navigation Model
 *fetches and prepare data for the navigation
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 *Class Navigation_Model
 *
 *@package model
 */
class Navigation_Model extends Model{
    
    /**
     * Holds the database reference
     * @var object
     */
    protected $oDBInstance;

       /**
     * Model constructor
     * Establishes connection to database
     */
    public function __construct(){
        
        $this->oDBInstance = Mysql_dbconnection::getInstance();
    }
/**
 * Gets the left navigation menu
 * @param integer language
 */
    public function getLeftNavigationTitle($iLanguage){
        $this->oDBInstance->query("SELECT content FROM bs_text_language WHERE name = 'leftnavigation' AND lid = '$iLanguage'");
        $aResult = $this->oDBInstance->fetch('array');
        return $aResult[0]['content'];
    }
    
    /**
     * Returns the page structure
     * @param integer page id of the current page
     * @param integer language of the title the that should be returned
     * @param string page typ condition
     */
    public function getPageStructure($iPid, $iLanguage, $sPageTypCondition= ''){
        $aPageStructure = array();
        $aPageStructure = $this->getSubPages($iPid, $iLanguage, $sPageTypCondition);

        if (!(empty($aPageStructure))) {
            for ($i =0; $i< sizeof($aPageStructure); $i++){
                $aPage = &$aPageStructure[$i];
                $aPage['subPages'] = $this->getPageStructure($aPage['id'], $iLanguage, $sPageTypCondition);

            }
        }
        return $aPageStructure;

    }
    /**
     * Returns the sub pages
     * @param integer page id of the current page
     * @param integer language of the title the that should be returned
     * @param string page typ condition
     */
    public function getSubPages($iPid, $iLanguage, $sPageTypCondition = ''){
        if($sPageTypCondition != ''){
            $sPageTypCondition = "AND $sPageTypCondition";
        }
        $this->oDBInstance->_setDefaultTable(
            	"(SELECT id, name, position, link, parent, level
            	  FROM bs_page INNER JOIN bs_page_language ON id = pid
            	  WHERE deleted = 0 AND visible = 1 AND cms_only = 0 AND lid = $iLanguage $sPageTypCondition) as a");
        $this->oDBInstance->_setDefaultColumn('id');

        $iParentID = $this->oDBInstance->getSQLValueString($iPid,'int');
        if ($iPid == 'NULL'){
            $this->oDBInstance->performSelect('id, name, link, level', '', "a.parent IS NULL ORDER BY position");
        }else {
            $this->oDBInstance->performSelect('id, name, link, level', '', "a.parent = '$iParentID' ORDER BY position");
        }
        return $this->oDBInstance->fetch('array');
    }

    /**
     * Gets all categories 
     * @param integer product id
     * @param integer language
     * @param string type page
     */
    public function getProdCat($iPid = '', $iLanguage, $sTyp = ''){
        $iParentID = $this->oDBInstance->getSQLValueString($iPid,'int');
        //If $Pid empty fetch all products
        if($iPid == ''){
            $this->oDBInstance->query("SELECT id, name, position, link, parent, level
            	  FROM bs_page INNER JOIN bs_page_language ON id = pid
            	  WHERE deleted = 0 AND visible = 1 AND cms_only = 0 AND lid = $iLanguage AND type = '$sTyp' ORDER BY position");
        }else{
            $this->oDBInstance->query("SELECT id, name, position, link, parent, level
            	  FROM (SELECT * FROM bs_page INNER JOIN bs_page_language ON id = pid
            	  WHERE deleted = 0 AND visible = 1 AND cms_only = 0 AND lid = $iLanguage AND type = '$sTyp') as p
            	  WHERE parent = '$iParentID' ORDER BY position");
        }

        $aProducts = $this->oDBInstance->fetch('array');

        if (!(empty($aProducts))) {
            for ($i =0; $i< sizeof($aProducts); $i++){
                $aProduct = &$aProducts[$i];
                $aProduct['subPages'] = $this->getProdCat($aProduct['id'], $iLanguage, 'category');

            }
        }
        return $aProducts;

    }
/**
 * Shows the current location in the homepage
 * @param integer product ID
 * @param integer language
 * @param string page type
 */
    public function getLocation($iPid, $iLanguage, $sTyp){
        $iPid = $this->oDBInstance->getSQLValueString($iPid,'int');
        $iLanguage = $this->oDBInstance->getSQLValueString($iLanguage,'int');

        $iCounter = 0;
        $aParents = array();
        $iParentID = '';
        $sParentName = '';
        $sParentLink = '';
        if($sTyp == 'product'){
            //Gets name of the product
            $this->oDBInstance->query("SELECT name FROM bs_product WHERE id=$iPid");
            $aParent = $this->oDBInstance->fetch('array');
            $sParentName = $aParent[0]['name'];
            $aParents[$iCounter] = array( "parent" => $sParentName, "link" => '');
            $iCounter++;

            //Gets parent information (category)
            $this->oDBInstance->query("SELECT pa.id, pl.name, pa.link FROM bs_product AS pr INNER JOIN bs_page AS pa
             ON pr.category = pa.id INNER JOIN bs_page_language AS pl ON pa.id = pl.pid WHERE pr.id='$iPid' AND pl.lid = '$iLanguage'");
            $aParent = $this->oDBInstance->fetch('array');
            //This is the parent id of the product parent
            $iParentID = $aParent[0]['id'];
            $sParentName = $aParent[0]['name'];
            $sParentLink = $aParent[0]['link'];
            $aParents[$iCounter] = array( "parent" => $sParentName, "link" => $sParentLink);
            $iCounter++;

            while($iParentID != ''){
                //Gets parent
                $this->oDBInstance->query("SELECT parent FROM bs_page WHERE id='$iParentID'");
                $aParent = $this->oDBInstance->fetch('array');
                $iParentID = $aParent[0]['parent'];

                //Gets parent information
                if ($iParentID != ''){
                    $this->oDBInstance->query("SELECT pa.id, pl.name, pa.link FROM bs_page AS pa
                INNER JOIN bs_page_language AS pl ON pa.id = pl.pid WHERE pa.id='$iParentID' AND pl.lid = '$iLanguage'");

                    $aParent = $this->oDBInstance->fetch('array');
                    $iParentID = $aParent[0]['id'];
                    $sParentName = $aParent[0]['name'];
                    $sParentLink = $aParent[0]['link'];

                    $aParents[$iCounter] = array( "parent" => $sParentName, "link" => $sParentLink);
                    $iCounter++;
                }

            }
        }elseif($sTyp == 'contentPage'){
            //Gets name of page
            $this->oDBInstance->query("SELECT name FROM bs_page as pa INNER JOIN bs_page_language as pl ON pa.id = pl.pid
             WHERE id=$iPid AND pl.lid = '$iLanguage'");
            $aParent = $this->oDBInstance->fetch('array');
            if(isset($aParent[0])){
            $sParentName = $aParent[0]['name'];
            $aParents[$iCounter] = array( "parent" => $sParentName, "link" => '');
            $iCounter++;
            $iParentID = $iPid;
            }

            //Gets parents
            while($iParentID != ''){
                //Gets parent
                $this->oDBInstance->query("SELECT parent FROM bs_page WHERE id='$iParentID'");
                $aParent = $this->oDBInstance->fetch('array');
                if(isset($aParent[0])){
                $iParentID = $aParent[0]['parent'];

                //Gets parent information
                if ($iParentID != ''){
                    $this->oDBInstance->query("SELECT pa.id, pl.name, pa.link FROM bs_page AS pa
                INNER JOIN bs_page_language AS pl ON pa.id = pl.pid WHERE pa.id='$iParentID' AND pl.lid = '$iLanguage'");

                    $aParent = $this->oDBInstance->fetch('array');
                    $iParentID = $aParent[0]['id'];
                    $sParentName = $aParent[0]['name'];
                    $sParentLink = $aParent[0]['link'];

                    $aParents[$iCounter] = array( "parent" => $sParentName, "link" => $sParentLink);
                    $iCounter++;
                }
                }
            }
        }
        return $aParents;
    }

}
?>